﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class GridManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // GridView1.ShowFooter = true;        // To display the footer
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "AddNewShipper":
                    GridView1.ShowFooter = true;
                    break;
                case "Cancel":
                    GridView1.ShowFooter = false;
                    lblError.Text = "";
                    break;
                case "Insert":

                    TextBox companyTextBox = GridView1.FooterRow.FindControl("txtNewCompanyName") as TextBox;
                    string newCompanyName = companyTextBox.Text;

                    TextBox phoneTextBox = GridView1.FooterRow.FindControl("txtNewPhone") as TextBox;
                    string newPhoneNum = phoneTextBox.Text;

                    try
                    {
                        SqlDataSource1.InsertParameters.Clear();
                        SqlDataSource1.InsertParameters.Add("CompanyName", newCompanyName);
                        SqlDataSource1.InsertParameters.Add("Phone", newPhoneNum);
                        SqlDataSource1.Insert();

                        GridView1.ShowFooter = false;
                    }
                    catch (Exception ex)
                    {
                        lblError.Text = ex.Message;
                    }
                    

                    break;
                default:
                    break;
            }

        }

        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            if (e.Exception == null)
            {
                // No exception has occurred
                // do not do anything
                lblError.Text = "";
            }
            else
            {
                // then, error has occurred
                // display the error message
                lblError.Text = e.Exception.Message;
                e.ExceptionHandled = true;

                // Retain the current record in EDIT mode
                e.KeepInEditMode = true;
            }
        }
    }
}